//
//  ControllView.m
//  NvSellerShow
//
//  Created by bmd on 2017/4/6.
//  Copyright © 2017年 Meicam. All rights reserved.
//

#import "ControllView.h"
#import "PPNumberButton.h"
#import "NvPlaySlider.h"
#import "NvsThumbnailSequenceView.h"
#import "SAVideoRangeSlider.h"

#import "NvMakeFilmClipModel.h"
#import "SaSliderModel.h"

@interface ControllView ()<SAVideoRangeSliderDelegate> {
    int time;
    NSNumber* _audioVolumn;
}

@property (weak, nonatomic) IBOutlet PPNumberButton *imageEditBtn;
- (IBAction)valueChange:(id)sender;
@property (weak, nonatomic) IBOutlet UILabel *soundNum;
@property (weak, nonatomic) IBOutlet NvPlaySlider *slider;
@property (weak, nonatomic) IBOutlet UIView *videoSlider;
@property (weak, nonatomic) IBOutlet UIView *imageSlider;
@property (weak, nonatomic) IBOutlet NvsThumbnailSequenceView *thumbnailSequence;
@property (weak, nonatomic) IBOutlet SAVideoRangeSlider *saSlider;
@property (weak, nonatomic) IBOutlet UILabel *fromLabel;
@property (weak, nonatomic) IBOutlet UILabel *toLabel;

@end
@implementation ControllView

-(void)awakeFromNib {
    [super awakeFromNib];
    self.imageEditBtn.minValue = 1;
    self.imageEditBtn.maxValue = 15;
    self.imageEditBtn.textColor = UIColorHexRGB(@"#333333");
    self.imageEditBtn.inputFieldFont = 23;
    self.imageEditBtn.backgroundColor = [UIColor whiteColor];
    self.saSlider.delegate = self;
    _audioVolumn = [NSNumber numberWithFloat:self.slider.value];
}

-(void)setModel:(NvMakeFilmClipModel *)model {
    _model = model;
    if(model.imageDuration.intValue != 0) {
        self.imageEditBtn.currentNumber = model.imageDuration.intValue;
    } else {
        self.imageEditBtn.currentNumber = 4;
    }
    time = (int)self.imageEditBtn.currentNumber;
    
    self.imageEditBtn.shakeAnimation = YES;
    ControllView __weak *weakSelf = self;
    self.imageEditBtn.resultBlock = ^(NSString *num){
        time = [num intValue];
        if ([weakSelf.delegate respondsToSelector:@selector(didChangeTime:)]) {
            [weakSelf.delegate didChangeTime:time];
        }
    };
    
    _audioVolumn = model.audioVolumn;
    if ([_audioVolumn floatValue]==-1||_audioVolumn==nil) {
        self.slider.value = 1.0;
    } else {
        self.slider.value = [_audioVolumn floatValue];
        self.soundNum.text = [NSString stringWithFormat:@"%ld%%",(NSInteger)(self.slider.value*50)];
    }
    
    if (model.clipType == NvClipVideo) {
        self.videoSlider.hidden = NO;
        self.imageSlider.hidden = YES;
        self.thumbnailSequence.mediaFilePath = self.model.clipPath;
    } else {
        self.videoSlider.hidden = YES;
        self.imageSlider.hidden = NO;
    }
}

-(void)setSliderModel:(SaSliderModel *)sliderModel {
    _sliderModel = sliderModel;
    self.saSlider.durationSeconds = _sliderModel.duration;
    self.saSlider.minGap = _sliderModel.triminTimeStamp;
    if (_sliderModel.trimoutTimeStamp.longLongValue == 0) {
        _sliderModel.trimoutTimeStamp = [NSNumber numberWithLongLong:_sliderModel.duration];
    }
    self.saSlider.maxGap = _sliderModel.trimoutTimeStamp;
    self.toLabel.text = [Utils stringWithTime:[_sliderModel.trimoutTimeStamp longLongValue]/NS_TIME_BASE];
    self.fromLabel.text = [Utils stringWithTime:[_sliderModel.triminTimeStamp longLongValue]/NS_TIME_BASE];
}

- (void)setStillImageHint:(BOOL)stillImageHint {
    _stillImageHint = stillImageHint;
    self.thumbnailSequence.stillImageHint = stillImageHint;
}

- (void)startTime:(int64_t)start duration:(int64_t)duration {
    self.thumbnailSequence.startTime = start;
    self.thumbnailSequence.duration = duration;
}

- (IBAction)valueChange:(NvPlaySlider *)sender {
    _audioVolumn = [NSNumber numberWithFloat:sender.value];
    self.soundNum.text = [NSString stringWithFormat:@"%ld%%",(NSInteger)(self.slider.value*50)];
    if ([self.delegate respondsToSelector:@selector(didChangeVolumn:)]) {
        [self.delegate didChangeVolumn:_audioVolumn];
    }
    NSLog(@"%f",sender.value);
}

- (void)videoRange:(id)videoRange didChangeLeftPosition:(CGFloat)leftPosition rightPosition:(CGFloat)rightPosition {
    if ([self.delegate respondsToSelector:@selector(videoRange:didChangeLeftPosition:rightPosition:)]) {
        [self.delegate videoRange:self.saSlider didChangeLeftPosition:leftPosition rightPosition:rightPosition];
    }
    NSLog(@"左边的值：%g",leftPosition);
    self.fromLabel.text = [Utils stringWithTime:leftPosition/NS_TIME_BASE];
    self.toLabel.text = [Utils stringWithTime:rightPosition/NS_TIME_BASE];
}

@end
